<?php

namespace App\Http\Controllers\Backend;


use App\Models\TopicCate;

class TopicController extends BaseController
{
    // 话题
    public function initialize()
    {
        parent::initialize();

        $this->model = 'topic';
    }

    // 父级
    // 添加，修改，删除，列表，停用 操作

    // 返回列表数据
    public function lists()
    {
        $where = $this->listWhere();
        $page  = $this->page;
        $rows  = $this->rows;
        $model = $this->checkModel($this->model);
        if($model === false){
            return $this->error($this->error);
        }

        // 模型 和 查询
        if($where){
            $result = $model->listPage($where, $page, $rows, "id DESC");
        } else{
            $result = $model->isCache(true)->listPage($where, $page, $rows, "id DESC");
        }

        // 获取栏目信息
        if($result['list']){
            $cateDb = new TopicCate();
            $cids   = array_column($result['list'], 'cate_id');
            $cates  = $cateDb->isCache(true)->getListInIds($cids);
            foreach($result['list'] as $key => $item){
                $_cid  = $item['cate_id'];
                $_cate = $cates[$_cid] ?? (object)[];

                // 整理
                $result['list'][$key]['cate'] = $_cate;
            }
        }

        return $this->success($result);
    }

    // 列表查询
    protected function listWhere()
    {
        $where   = [['status', '>=', '0']];
        $cate_id = $this->getData('cate_id', null, 'intval');
        $keyword = $this->getData('keyword', null, 'trim');
        if($keyword){
            $where[] = $this->orWhere('title|describe|tag', 'like', $keyword);
        }
        if(!is_null($cate_id)){
            $where[] = ['cate_id', '=', $cate_id];
        }

        return $where;
    }

    // 获取关注话题的用户列表
    public function users()
    {
        $t_id = $this->getData('topic_id', 0, 'intval');
        $this->throwIf(empty($t_id), "请指定话题编号！！");

        // 参数
        $page  = $this->page;
        $rows  = $this->rows;
        $model = $this->checkModel('TopicSubscribe');
        if($model === false){
            return $this->error($this->error);
        }

        // 模型 和 查询
        $result = $model->getUsers(["topic_id" => $t_id], $page, $rows, "id DESC");

        return $this->success($result);
    }

    // 启用禁用
    public function status()
    {
        $_value = $this->getData('status', 0, 'intval');
        $this->throwIf(is_null($_value), '是否启用状态必须填写！');
        $this->throwIf(!in_array($_value, [0, 1]), '是否启用状态必须为0或1！');

        return $this->changeField('status', $_value);
    }

    // 是否推荐
    public function propose()
    {
        $_value = $this->getData('is_propose', 0, 'intval');
        $this->throwIf(is_null($_value), '是否启用状态必须填写！');
        $this->throwIf(!in_array($_value, [0, 1]), '是否启用状态必须为0或1！');

        return $this->changeField('is_propose', $_value);
    }

    // 查找
    public function show()
    {
        $this->throwIf(empty($this->id), '请指定查看记录');

        $where = ['id' => $this->id];
        $model = $this->checkModel($this->model);
        $info  = $model->isCache(true)->oneFind($where);
        $this->throwIf($info === false, $model->getError());

        // 数据整理
        $info['image'] = $this->getFullUrl($info['image']);
        if($info['cate_id']){
            $cateDb = new TopicCate();
            $cate   = $cateDb->isCache(true)->getOneById($info['cate_id']);
            if(empty($cate)){
                $cate = (object)[];
            }
            // 整理
            $info['cate'] = $cate;
        }

        return $this->success($info);
    }

    // 删除（软删除）
    public function del()
    {
        $this->throwIf(empty($this->id), '请指定删除记录');

        $where  = ['id' => $this->id];
        $model  = $this->checkModel($this->model);
        $result = $model->isCache(true)->oneEdit($where, ['status' => -1]);
        $this->throwIf($result === false, $model->getError());

        return $this->success();
    }

    // 添加
    protected function _before_add()
    {
        $data = [
            //'follow_no'         => $this->getData('follow_no', 0, 'intval'),
            //'share_no'          => $this->getData('share_no', 0, 'intval'),
            //'virtual_follow_no' => $this->getData('virtual_follow_no', 0, 'intval'),
            'title'      => $this->getData('title', null, 'trim'),
            'image'      => $this->getData('image', null, 'trim'),
            'describe'   => $this->getData('describe', null, 'trim'),
            'tag'        => $this->getData('tag', null, 'trim'),
            'cate_id'    => $this->getData('cate_id', 0, 'intval'),
            'is_propose' => $this->getData('is_propose', 0, 'intval'),
            'rank'       => $this->getData('rank', 0, 'intval'),
            'status'     => $this->getData('status', 1, 'intval'),
            'verify'     => $this->getData('verify', 1, 'intval'),
        ];

        return $data;
    }

    // 修改
    protected function _before_edit()
    {
        $data = [
            //'follow_no'         => $this->getData('follow_no', 0, 'intval'),
            //'share_no'          => $this->getData('share_no', 0, 'intval'),
            //'virtual_follow_no' => $this->getData('virtual_follow_no', 0, 'intval'),
            'title'      => $this->getData('title', null, 'trim'),
            'image'      => $this->getData('image', null, 'trim'),
            'describe'   => $this->getData('describe', null, 'trim'),
            'tag'        => $this->getData('tag', null, 'trim'),
            'cate_id'    => $this->getData('cate_id', 0, 'intval'),
            'is_propose' => $this->getData('is_propose', 0, 'intval'),
            'rank'       => $this->getData('rank', 0, 'intval'),
            'status'     => $this->getData('status', 1, 'intval'),
            'verify'     => $this->getData('verify', 1, 'intval'),
        ];

        return $data;
    }

}


